//
// Copyright (c) 2001-2003 BroadVision, Inc. All rights reserved.
//
// This software is copyrighted.  Under the copyright laws, this software
// may not be copied, in whole or in part, without prior written consent
// of BroadVision, Inc. or its assignees.  This software is provided under
// the terms of a license between BroadVision and the recipient, and its
// use is subject to the terms of that license.
//
// This software may be protected by one or more U.S. and International
// patents.  Certain applications of BroadVision One-To-One software are
// covered by U.S. patent 5,710,887.
//
// TRADEMARKS: BroadVision and BroadVision One-To-One are registered
// trademarks of BroadVision, Inc., in the United States and the European
// Community, and are trademarks of BroadVision, Inc., in other
// countries.  The BroadVision logo, is a trademark of BroadVision, Inc.,
// in the United States and other countries. Additionally, IONA and Orbix
// are trademarks of IONA Technologies, Ltd.  RSA, MD5, and RC2 are
// trademarks of RSA Data Security, Inc.
//

//
// Create a dummy (timestamp) attribute to get around an IE cache problem.
//
function getTimeStamp() {
   var now = new Date;
   var timestamp = '' + now.getYear() + now.getMonth() + now.getDate() + now.getHours() + now.getMinutes() + now.getSeconds() + now.getMilliseconds();
   return timestamp;
}

//
// popup new window to find and select a document type
//
function findDocTypePopup(popupUrl, parentFormName, parentFieldName) {

  var timestamp = getTimeStamp();

  if (popupUrl != null) {
    if (popupUrl.indexOf("?") < 0) {
      popupUrl += "?";
    } else {
      popupUrl += "&";
    }
  }
  
  popupUrl += 'timestamp=' + timestamp +
              '&parentFormName=' + parentFormName +
              '&parentFieldName=' + parentFieldName;

  var windowName = parentFormName + parentFieldName;
  var windowFeatures = 'width=700,height=600,scrollbars=yes,resizable=yes';

  var popupWindow = window.open(popupUrl, windowName, windowFeatures);

  if ( popupWindow != null) {
    if( popupWindow.opener == null) {
      popupWindow.opener = self;
    }
  }
  popupWindow.focus();
}

//
// popup new window to find and select a process pack and a process model
//
function findProcessPopup(popupUrl, parentFormName, parentFieldName) {

  var timestamp = getTimeStamp();

  if (popupUrl != null) {
    if (popupUrl.indexOf("?") < 0) {
      popupUrl += "?";
    } else {
      popupUrl += "&";
    }
  }
  
  popupUrl += 'timestamp=' + timestamp +
              '&parentFormName=' + parentFormName +
              '&parentFieldName=' + parentFieldName;

  var windowName = parentFormName + parentFieldName;
  var windowFeatures = 'width=700,height=600,scrollbars=yes,resizable=yes';

  var popupWindow = window.open(popupUrl, windowName, windowFeatures);

  if ( popupWindow != null) {
    if( popupWindow.opener == null) {
      popupWindow.opener = self;
    }
  }
  popupWindow.focus();
}

//
// popup new window to find and select a user
//
function findUserPopup(popupUrl, parentFormName, parentFieldName) {

  var timestamp = getTimeStamp();

  if (popupUrl != null) {
    if (popupUrl.indexOf("?") < 0) {
      popupUrl += "?";
    } else {
      popupUrl += "&";
    }
  }

  var pV = "document." + parentFormName + "." + parentFieldName + ".value";
  var parentFieldValue = eval(pV);

  popupUrl += 'timestamp=' + timestamp +
              '&parentFormName=' + parentFormName +
              '&parentFieldName=' + parentFieldName +
              '&userAlias=' + parentFieldValue;
  if ((null != parentFieldValue) && ("" != parentFieldValue)){
    popupUrl += '&executeSearch=true';
  }

  var windowName = parentFormName + parentFieldName;
  var windowFeatures = 'width=700,height=600,scrollbars=yes,resizable=yes';

  var popupWindow = window.open(popupUrl, windowName, windowFeatures);

  if ( popupWindow != null) {
    if( popupWindow.opener == null) {
      popupWindow.opener = self;
    }
  }
  popupWindow.focus();
}

//
// popup new window to find and select multiple users and groups
//
function findUsersAndGroupsPopup(popupUrl, parentFormName, parentFieldName, parentFieldRow, packageId, actorId) {

  var timestamp = getTimeStamp();

  if (popupUrl != null) {
    if (popupUrl.indexOf("?") < 0) {
      popupUrl += "?";
    } else {
      popupUrl += "&";
    }
  }

  popupUrl += 'timestamp=' + timestamp +
              '&parentFormName=' + parentFormName +
              '&parentFieldName=' + parentFieldName +
              '&parentFieldRow=' + parentFieldRow +
              '&packageId=' + packageId +
              '&actorId=' + actorId;
  var windowName = parentFormName + parentFieldName + parentFieldRow;
  var windowFeatures = 'width=700,height=600,scrollbars=yes,resizable=yes';

  var popupWindow = window.open(popupUrl, windowName, windowFeatures);

  if ( popupWindow != null) {
    if( popupWindow.opener == null) {
      popupWindow.opener = self;
    }
  }
  popupWindow.focus();
}

//
// 'callback' function to the parentWindow to set the selected fieldValue
//
function setFieldValue(refreshUrl, formName, fieldName, fieldValue) {

  var thisForm = document.forms[formName];
  var elements = thisForm.elements;
  var numElements = elements.length;

  var field;

  for (var elementIndex = 0; elementIndex < numElements; elementIndex++) {
    var elementName = elements[elementIndex].name;
    if (elementName == fieldName) {
      field = elements[elementIndex];
      break;
    }
  }

  if (field.type == 'text') {
    field.value = fieldValue;
  }
  else if (field.type == 'hidden') {
    refreshParentWindow(refreshUrl, fieldName, fieldValue);
  }
}

function setFieldValueSimple(formName, fieldName, fieldValue) {

  var thisForm = document.forms[formName];
  var elements = thisForm.elements;
  var numElements = elements.length;

  var field;

  for (var elementIndex = 0; elementIndex < numElements; elementIndex++) {
    var elementName = elements[elementIndex].name;
    if (elementName == fieldName) {
      field = elements[elementIndex];
      break;
    }
  }

 field.value = fieldValue;

}


function refreshParentWindow(refreshUrl, fieldName, fieldValue)
{
  if (refreshUrl != null) {
    if (refreshUrl.indexOf("?") < 0) {
      refreshUrl += "?";
    } else {
      refreshUrl += "&";
    }
  }
  window.location = refreshURL + '.do' + fieldName + '=' + fieldValue;
}
